﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using CharacterTrailLibrary.Exceptions;
using CharacterTrailLibrary.Interface;

namespace CharacterTrailLibrary.Impl
{
    public class UserLoader : IUserLoader
    {
        //位序号	说明
        //1	工序代码管理
        //2	Bom维护
        //3	串码
        //4	维修
        //5	串码跟踪
        //6	Bom审核、反审核
        private string _roles = "111111";
        public const string Wrklog = "wrklog.dll";
        private string[] _wrklogLines = null;
        public UserLoader()
        {
            try
            {
                _wrklogLines = File.ReadAllLines(Wrklog);
                if(_wrklogLines.Length >=4)
                {
                    _roles = _wrklogLines[3];
                }
            }
            catch (Exception ex)
            {
                throw new AppException(String.Format("读取文件{0}发生错误！\n\r",Wrklog) + ex.Message);
            }
        }

        public string GetUserId()
        {
            return _wrklogLines[0];
        }

        public string GetUserName()
        {
            return _wrklogLines[1];
        }

        public string GetPassword()
        {
            return String.Empty;
        }

        public string GetSqlConnectionString()
        {
            return _wrklogLines[2];
        }

        /// <summary>
        /// 根据序号获得权限，从1开始
        /// </summary>
        /// <param name="index">从1开始</param>
        /// <returns>是否具有权限</returns>
        public bool GetRoleByIndex(int index)
        {
            if(_roles.Length>=index )
            {
                return (_roles[index - 1] == '1');
            }
            return false;
        }
    }
}
